class Solution(object):
    def findRLEArray(self, encoded1, encoded2):
        n1 = len(encoded1)
        n2 = len(encoded2)
        left = 0
        right = 0
        ans = []
        while left < n1 and right < n2:
            if encoded1[left][1] == encoded2[right][1]:
                x, y = encoded1[left][0] * encoded2[right][0], encoded1[left][1]
                left += 1
                right += 1
            else:
                if encoded1[left][1] > encoded2[right][1]:
                    x, y = encoded1[left][0] * encoded2[right][0], encoded2[right][1]
                    encoded1[left][1] -= encoded2[right][1]
                    right += 1
                else:
                    x, y = encoded1[left][0] * encoded2[right][0], encoded1[left][1]
                    encoded2[right][1] -= encoded1[left][1]
                    left += 1
            if ans and ans[-1][0] == x:
                ans[-1][1] += y
            else:
                ans.append([x, y])
        return ans


data = Solution()
encoded1 = [[1, 3], [2, 1], [3, 2]]
encoded2 = [[2, 3], [3, 3]]
print(data.findRLEArray(encoded1, encoded2))
